package ch.fhnw.qualiscan.application.elements;

import java.util.Arrays;
import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.core.helpers.TextHelper;

import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class BestPracticeForm extends VerticalLayout {
	
	public RichTextArea editor;
	public Label title;
	public Label desc;
	public TextField titleTextField;
	public OptionGroup visibility;
	
	public TextHelper textHelper;
	
	public BestPracticeForm() {
		textHelper = new TextHelper();
		title = textHelper.h2(TM.get("bestpractice.title"));
		desc = textHelper.p(TM.get("bestpractice.desc"));
		
		buildElement();
	}
	
	public BestPracticeForm(String domainTitle) {
		textHelper = new TextHelper();
		desc = textHelper.p(TM.get("bestpractice.desc2", domainTitle));
		title = null;
		buildElement();
	}
	
	public void buildElement() {
		
		editor = new RichTextArea();
		editor.setWidth("100%");

		titleTextField = new TextField();
		titleTextField.setColumns(25);
		
		List<Integer> weights = Arrays.asList(new Integer[] {0,1});
		
		visibility = new OptionGroup(null, weights);
		visibility.setWidth("");
		visibility.select(1);
		visibility.addStyleName("h-optiongroup visibility");
		visibility.setItemCaption(0, TM.get("bestpractice.form.public"));
		visibility.setItemCaption(1, TM.get("bestpractice.form.private"));
	
		VerticalLayout mainWrap = new VerticalLayout();
		mainWrap.setMargin(true, false, true, false);
		mainWrap.setSpacing(true);
		
		if (title != null) {
			addComponent(title);
		}
		
		addComponent(desc);
		
		HorizontalLayout titleWrap = new HorizontalLayout();
		
		Label titleLabel = textHelper.span(TM.get("bestpractice.form.title"));
		titleLabel.setWidth("160px");
		titleWrap.addComponent(titleLabel);
		titleWrap.addComponent(titleTextField);
		
		HorizontalLayout statusWrap = new HorizontalLayout();
		Label statusLabel = textHelper.span(TM.get("bestpractice.form.visibility"));
		statusLabel.setWidth("160px");
		statusWrap.addComponent(statusLabel);
		statusWrap.addComponent(visibility);
		Button helpButton = new Button("");
		helpButton.setDescription(TM.get("bestpractice.form.help"));
		helpButton.setStyleName(BaseTheme.BUTTON_LINK);
		helpButton.setIcon(Config.ICON_HELP);
		statusWrap.addComponent(helpButton);
		
		HorizontalLayout textWrap = new HorizontalLayout();
		Label textLabel = textHelper.span(TM.get("bestpractice.form.textbox"));
		textLabel.setWidth("160px");
		textWrap.addComponent(textLabel);
		textWrap.addComponent(editor);
		
		mainWrap.addComponent(titleWrap);
		mainWrap.addComponent(statusWrap);
		mainWrap.addComponent(textWrap);

		addComponent(mainWrap);
	}
	
	public String getTextValue() {
		return editor.getValue().toString();
	}
	
	public String getTitleValue() {
		return (String)titleTextField.getValue();
	}
	
	public int getPublicValue() {
		return Integer.valueOf(visibility.getValue().toString()).intValue();
	}
	
	public void setValue(String text, String title, int publicpost) {
		editor.setValue(text);
		titleTextField.setValue(title);
		this.visibility.select(publicpost);
	}
	
}
